Skip to content

refactor: hoist regexes into LazyLock statics (+ fix handle_reply init bug)#604

Merged
nolik merged 1 commit into
mainfrom
iter3/lazylock-regexes
Jul 18, 2026
Merged

refactor: hoist regexes into LazyLock statics (+ fix handle_reply init bug)#604
nolik merged 1 commit into
mainfrom
iter3/lazylock-regexes

Conversation

@nolik

@nolik nolik commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

Iteration 3/8 of the hardening plan (§1/§4 — regex init cleanup).

  • The five MentionParameters regexes and the two chat_gpt_handler statics (BOT_PROFILES, CHAT_SUMMARY_REQUEST_REGEX) now use LazyLock instead of eager Regex::new(...) per default() / OnceLock + get_or_init. Each pattern compiles exactly once.
  • The remaining expects are now isolated to single constant-initializer sites — they can only fire on a developer typo, never at runtime — which sets up the strict no-panic lint (Iteration 8).

Bug fix (free): handle_reply previously read BOT_PROFILES.get() and silently no-op'd if a reply arrived before the first handle_chat_gpt_question had lazily initialized the profiles. With LazyLock the profiles are always populated, so the if let Some(...) guard is removed and replies work regardless of ordering.

Pure refactor — no happy-path behavior change. Local gate green (fmt, clippy -D warnings, 8 unit tests). e2e (real handler tree) is the CI backstop.

🤖 Generated with Claude Code

Compiles each regex exactly once instead of on every
`MentionParameters::default()` / handler invocation, and isolates the
remaining `expect`s to single constant-initializer sites (they can only
fire on a developer typo, never at runtime), which sets up the
no-panic lint in a later iteration.

- boot.rs: the five MentionParameters regexes now clone from
  `LazyLock<Regex>` statics.
- chat_gpt_handler.rs: `BOT_PROFILES` and `CHAT_SUMMARY_REQUEST_REGEX`
  move from `OnceLock` (+ `get_or_init`) to `LazyLock`.

Also fixes a latent bug: `handle_reply` read `BOT_PROFILES.get()` and
silently did nothing if a reply arrived before the first
`handle_chat_gpt_question` had initialized the profiles. With `LazyLock`
the profiles are always populated, so the guard is gone.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@nolik
nolik merged commit c001ab9 into main Jul 18, 2026
7 checks passed
@nolik
nolik deleted the iter3/lazylock-regexes branch July 18, 2026 13:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant